(function() {
    angular.module('BlurAdmin.pages.redAdminM')
        .controller('RedAdminMCtrl', RedAdminMCtrl);

    function RedAdminMCtrl($scope, $ShareData, $filter, $uibModal, $datepicker, taRegisterTool, $openModal, $myconfig, $http, $mydialog) {
        //权限判断
        $scope.is_suer_root = is_suer_root;
        //弹窗框
        $scope.open = $openModal; //创建实例化对象
        function fn(val) { //关闭模态框方法
            $scope.myopen.opt_close = val;
        }
        $scope.myopen = function(page, size) { //打开模态框 不可关闭
            $scope.open.newOpen(page, size, $scope, fn, 'static');
        }
        $scope.myopen2 = function(page, size) { //打开模态框 ,可关闭
            $scope.open.newOpen(page, size, $scope, fn);
        }

        // $scope.myopen('app/pages/redAdminM/newRed/newRed.html', 'lg');
        $scope.list_data_opt = {
            "page": 1, //当前页数 true
            limit: 5, //每页显示多少条 true
            "name": "", //红包名称
            "start_time": "", //红包开始时间
            "end_tine": "", //红包结束时间
            "birthdaystart": "", //生日开始时间
            "birthdayend": "", //生日结束时间
            status: "all", //红包状态 0未开始1进行中3停止4已结束';
            "surplus": "all" // 红包是否已抢光（true：是，false：否，全部，不传）
        };

        //日期插件
        $scope.format = "yyyy/MM/dd";
        $scope.altInputFormats = ['yyyy-MM-dd-HH'];
        $scope.dat = {
            dat1: $datepicker.dataNew(),
            dat2: $datepicker.dataNew(),
            dat3: $datepicker.dataNew(),
            dat4: $datepicker.dataNew(),
        };
        $scope.dat.dat1.date_ = null;
        $scope.dat.dat2.date_ = null;
        $scope.dat.dat3.date_ = null;
        $scope.dat.dat4.date_ = null;

        //查看活动配图
        $scope.ope_img = function(val, text) {
            console.log(val);
            $scope.popimg = val;
            $scope.popimg_text = text;
            $scope.open.newOpen('app/pages/redAdminM/img-pup.html', 'md', $scope);
        }

        //查看红包条件
        $scope.red_conditionF = function(opt) {
            $scope.imposed = opt;
            console.log(opt);
            $scope.open.newOpen('app/pages/redAdminM/red_condition.html', 'md', $scope);
        }

        // $scope.myopen2('app/pages/redAdminM/redDetails/redDetails.html', 'lg');
        // $scope.myopen('app/pages/redAdminM/WinningRecord/WinningRecord.html', 'lg');

        $scope.search_data = function() { //点击搜索按钮
            $scope.list_data_opt.page = 1;
            list_num();
        };

        //列表数据统计
        var add_text = ''; //定义地址搜索
        var date_ = {
            dat1: null,
            dat2: null,
            dat3: null,
            dat4: null
        };

        function list_num() {
            add_text = ''

            //获取时间
            date_.dat1 = document.getElementById("dat1") ? document.getElementById("dat1").value : null;
            date_.dat2 = document.getElementById("dat2") ? document.getElementById("dat2").value : null;
            date_.dat3 = document.getElementById("dat3") ? document.getElementById("dat3").value : null;
            date_.dat4 = document.getElementById("dat4") ? document.getElementById("dat4").value : null;

            var url = $myconfig.api.redpacket_redpacket_count_b;
            var opt = {};
            var opt2 = $scope.list_data_opt;
            opt2.name && (opt.name = opt2.name); //红包名称
            date_.dat1 && (opt.start_time = date_.dat1); //红包开始时间
            date_.dat2 && (opt.end_tine = date_.dat2); //红包结束时间
            date_.dat3 && (opt.birthdaystart = date_.dat3); //生日开始时间
            date_.dat4 && (opt.birthdayend = date_.dat4); //生日结束时间
            //红包状态 all是所有红包，不传给后台，0未开始1进行中3停止4已结束';
            if (opt2.status && !(opt2.status == 'all')) {
                opt.status = opt2.status
            }

            //地址搜索
            var add1 = $('#add1 option:selected').text();
            var add2 = $('#add2 option:selected').text();
            var add3 = $('#add3 option:selected').text();
            add1 && (add1 != '省') && (add_text += add1);
            add2 && (add2 != '市') && (add_text += ('-' + add2));
            add3 && (add3 != '区') && (add_text += ('-' + add3));
            add_text && (opt.area = add_text);


            if (opt2.surplus != 'all') {
                opt.surplus = opt2.surplus;

            }
            if (opt2.status != 'all') {
                opt.status = opt2.status;
            }

            $http.get(url, { params: opt })
                .then(function(res) {
                    $scope.bigTotalItems = res.data.data ? res.data.data : 0;
                    list_data();
                });
        }

        //列表数据
        function list_data() {
            var opt = {};
            var opt2 = $scope.list_data_opt;
            opt.page = opt2.page;
            opt.limit = opt2.limit;


            //获取时间
            date_.dat1 = document.getElementById("dat1") ? document.getElementById("dat1").value : null;
            date_.dat2 = document.getElementById("dat2") ? document.getElementById("dat2").value : null;
            date_.dat3 = document.getElementById("dat3") ? document.getElementById("dat3").value : null;
            date_.dat4 = document.getElementById("dat4") ? document.getElementById("dat4").value : null;

            opt2.name && (opt.name = opt2.name); //红包名称
            date_.dat1 && (opt.start_time = date_.dat1); //红包开始时间
            date_.dat2 && (opt.end_tine = date_.dat2); //红包结束时间
            date_.dat3 && (opt.birthdaystart = date_.dat3); //生日开始时间
            date_.dat4 && (opt.birthdayend = date_.dat4); //生日结束时间
            //红包状态 all是所有红包，不传给后台，  0未开始1进行中3停止4已结束';
            if (opt2.status && !(opt2.status == 'all')) {
                opt.status = opt2.status
            }
            add_text && (opt.area = add_text); //判断是否有地区的值

            var OO = JSON.stringify(opt);
            var opt3 = JSON.parse(OO);

            if (opt2.surplus != 'all') {
                opt.surplus = opt2.surplus;
            }
            if (opt2.status != 'all') {
                opt.status = opt2.status;
            }
            $http.get($myconfig.api.redpacket_redpacket_list_b, { params: opt })
                .then(function(res) {
                    if (res.data.success) {
                        var opt = res.data.data;
                        for (var i = 0; i < opt.length; i++) {
                            res.data.data[i].num = (($scope.list_data_opt.page - 1) * $scope.list_data_opt.limit) + i + 1;
                            if (opt[i].area.length > 0 || opt[i].sex != 3 || opt[i].birthdaystart || opt[i].birthdayend) {
                                var imposed = {
                                    area: opt[i].area,
                                    sex: opt[i].sex,
                                    birthdaystart: opt[i].birthdaystart,
                                    birthdayend: opt[i].birthdayend,
                                };
                                opt[i].is_imposed = true;
                                opt[i].imposed = imposed;
                            } else {
                                opt[i].is_imposed = false;
                            }
                        }
                        $scope.list_datas = res.data.data;
                        console.log($scope.list_datas);
                    } else {
                        $mydialog.fail(res.data.data);
                    }
                });
        }
        $scope.search_data();

        //点击分页事件
        $scope.maxSize = 5;
        $scope.bigTotalItems = 50;
        $scope.pageChanged = function(val) {
            console.log(val);
            var Page_num = val //选中的页面数
            $scope.list_data_opt.page = val;
            list_data();
        };

        //红包详情
        $scope.Details = function(val) {
                $ShareData.setRed(val);
                $scope.myopen2('app/pages/redAdminM/redDetails/redDetails.html', 'lg');
            }
            // $scope.myopen2('app/pages/redAdminM/redDetails/redDetails.html', 'lg');

        $scope.red_list_Winning = function(val) {
                $ShareData.setRed(val);
                $scope.myopen2('app/pages/redAdminM/WinningRecord/WinningRecord.html', 'lg');
            }
            //红包推荐、取消推荐
        $scope.redpacket = function(val, type) {
            var opt = {
                "redpacket_id": val,
                "issuggest": !type
            };
            $http.post($myconfig.api.redpacket_redpacket_recommend, opt)
                .then(function(res) {
                    console.log(res);
                    if (res.data.success) {
                        $mydialog.success("修改成功");
                        list_data();
                    } else {
                        $mydialog.fail("修改失败");
                    }
                })
        };

        //修改红包状态，终止红包
        $scope.Termination = function(val, type) {
            $http.post($myconfig.api.redpacket_redpacket_end, {
                    redpacket_id: val,
                    status: type
                })
                .then(function(res) {
                    console.log(res);
                    if (res.data.success) {
                        $mydialog.success("修改成功");
                        list_data();
                    } else {
                        $mydialog.fail("修改失败");
                    }
                })
        }


        //激活红包 弹出框
        var $ctrl = this;

        function jihuo(type, val) {
            if (type) {
                $scope.jihuo_code(val);
            }
        }
        var time = 60;
        $scope.activation = function(val, fn) {
            $scope.red_id = val;
            var fn = jihuo;
            $ctrl.animationsEnabled = true;
            var open = $uibModal.open({
                animation: $ctrl.animationsEnabled,
                backdrop: 'static',
                size: 'sm',
                ariaLabelledBy: 'modal-title',
                ariaDescribedBy: 'modal-body',
                template: '<div class="alert"><div class="modal-header"><h4 style="color: #949494;">请输入验证码<span ng-if="time>0">{{time}}</span></h4><input type="text" ng-model="tel_code" class="form-control ng-pristine ng-untouched ng-valid ng-not-empty"></div><div class="row">&nbsp;</div><div class="modal-footer"><button type="button" class="btn btn-default" ng-click="_inquiry_fn(false)">取消</button><button type="button" class="btn btn-primary" ng-click="_inquiry_fn(true)">确定</button ></div></div>',
                controllerAs: '$ctrl',
                controller: function($scope) {
                    $scope.time = time;
                    t = setInterval(function() {
                        $scope.$apply(function() {
                            $scope.time = time;
                        });
                    }, 1000);

                    $scope.is_inquiry = null;
                    $scope._inquiry_fn = function(type) {
                        if (isNaN($scope.tel_code) && type) {
                            return $mydialog.fail("请输入数字验证码");
                        }
                        $scope.is_inquiry = type;
                        open.close();
                        fn($scope.is_inquiry, $scope.tel_code);
                    }

                }
            });
            //获取验证码接口
            if (time == 60) {
                yanzcode();
            }
        }

        function yanzcode() {
            t = setInterval(function() {
                time = time - 1;
                console.log(time);
                if (time < 0) {
                    clearInterval(t);
                    time = 60;
                }
            }, 1000);
            $http.post($myconfig.api.my_code, {
                    mobile: $myconfig.api.admin_tel,
                    code_type: "redpacket",
                    redpacket_id:$scope.red_id 
                })
                .then(function(res) {
                    if (res.data.success) {
                        $mydialog.success("验证码已发送，请输入验证码");
                    }
                })
        }

        //激活红包验证
        $scope.jihuo_code = function(val) {
            var opt = {
                "mobile": $myconfig.api.admin_tel,
                "code_type": "redpacket",
                "code": val
            };
            $http.post($myconfig.api.redpacket_admin_verify, opt).then(function(res) {
                if (res.data.success) { //验证成功后改变红包状态为2
                    $scope.Termination($scope.red_id, '2');
                }
            });

        }

        //修改红包状态




    }
})();